'use strict';

const path = require('path');
const fs = require('fs').promises;
const sequelize = require('../shared/utils/database');
const Sequelize = require('sequelize');

/**
 * 运行所有迁移文件
 */
async function runMigrations() {
  try {
    console.log('开始执行数据库迁移...');
    
    // 获取迁移文件目录
    const migrationsDir = path.join(__dirname);
    
    // 读取迁移文件
    const files = await fs.readdir(migrationsDir);
    
    // 过滤出.js文件但排除当前文件
    const migrationFiles = files.filter(file => 
      file.endsWith('.js') && file !== 'run.js' && file !== 'init.js'
    );
    
    console.log(`找到 ${migrationFiles.length} 个迁移文件`);
    
    // 按顺序执行迁移
    for (const file of migrationFiles) {
      console.log(`正在执行迁移: ${file}`);
      const migration = require(path.join(migrationsDir, file));
      await migration.up(sequelize.getQueryInterface(), Sequelize);
      console.log(`迁移 ${file} 执行成功`);
    }
    
    console.log('所有迁移执行完成');
    process.exit(0);
  } catch (error) {
    console.error('执行迁移时出错:', error);
    process.exit(1);
  }
}

// 运行迁移
runMigrations(); 